package gzx.datalayer.Data.TotalSearch
{
	import gzx.mgroup_totalsearch.modules.Yu_SearchParams;
	
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class WSearch
	{
		private var remoteObject:RemoteObject
		private var model:WSearchModel; 
		
		public var onReceive_GetWInfoForInitPage:Function;
		public var onReceive_QueryTotalList:Function;
		
		public var onErrorFunction:Function;
		
		public function WSearch( model:WSearchModel = null )
		{
			remoteObject  = new RemoteObject("fluorine");
			remoteObject.source = "RemoteFlunorineFx.TotalSearch.WSearch";
			
			remoteObject.GetWInfoForInitPage.addEventListener("result",GetWInfoForInitPage_ResultHandler);
			remoteObject.QueryTotalList.addEventListener("result",QueryTotalList_ResultHandler);
			
			remoteObject.addEventListener("fault", onFault);
			if( model == null )
				model = new WSearchModel();
			
			this.model = model;
			onReceive_GetWInfoForInitPage = null;
			onErrorFunction = null;
		}
		
		//====================
		public function GetWInfoForInitPage(responder:IResponder = null ):void
		{
			var asyncToken:AsyncToken = remoteObject.GetWInfoForInitPage();
			
			if( responder != null )
				asyncToken.addResponder(responder);
		} 
		public function GetWInfoForInitPage_ResultHandler(event:ResultEvent):void
		{
			var returnValue:Object = event.result as Object;
			model.GetWInfoForInitPage_Result = returnValue;
			
			if(null != onReceive_GetWInfoForInitPage)
				onReceive_GetWInfoForInitPage();
		}
		
		//====================
		public function QueryTotalList(searchPrm:Yu_SearchParams,responder:IResponder = null ):void
		{
			var asyncToken:AsyncToken = remoteObject.QueryTotalList(searchPrm);
			
			if( responder != null )
				asyncToken.addResponder(responder);
		} 
		public function QueryTotalList_ResultHandler(event:ResultEvent):void
		{
			var returnValue:Object = event.result as Object;
			model.QueryTotalList_Result = returnValue;
			
			if(null != onReceive_QueryTotalList)
				onReceive_QueryTotalList();
		}
		
		//=========================
		public function onFault (event:FaultEvent):void
		{
			if(onErrorFunction!=null)
			{
				onErrorFunction();
			}
			else
			{
				Alert.show(event.fault.faultString, "Error");
			}
		}
	}
}